package com;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileAppender;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import org.apache.commons.lang3.ObjectUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Python {

    private final static Log log = Log.get();

    static Set<String> set = new HashSet<>();

    private final static ThreadPoolExecutor executorMiddleService = new ThreadPoolExecutor(
            2, 3,
            60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());

    public static void main(String[] args) {
//        System.out.println(HttpUtil.get(" http://youke.cn-idvhliy.qiniudns.com.zhaowho.cn/qq.php?t=163801067875232"));
        System.out.println(set.add("1"));
        System.out.println(set.add("1"));
        System.out.println(set.add("1"));
        String url = "http://youke.cn-idvhliy.qiniudns.com.zhaowho.cn/qq.php?t=163801067875232";
//        String url = "http://wuci.cn-idvhliy.qinfttg.cn.zhaowho.cn/qq.php?t=163801067875232";
        http(url, "163801067875232", true);

    }

    public static void http(String soap, String id, Boolean bool){
        String filUrl = "d:/sss/%s/%s.png";
        String str = HttpUtil.get(soap);
        String[] arr = str.split("\n");
        String  title = null;
        int index = 0;

        Document document = Jsoup.parse(str);
        String titleStr = Xsoup.select(document,"/html/head/title").get();
        if (titleStr.contains("<title>")) {
            title = titleStr.substring(titleStr.indexOf("<title>")+7, titleStr.lastIndexOf("</title"));
            title = title.trim();
            File titleFile = new File(String.format(filUrl,title,0));
            if (!titleFile.getParentFile().exists()) {
                titleFile.getParentFile().mkdirs();
            }
        }
        List<String> imagesUrls = Xsoup.select(document,"//*[@class=\"note-content\"]/p/img/@src").list();
        for (String imgUrl: imagesUrls) {
            if (!imgUrl.contains("\"")){
                if (bool) {
                    try {
                        String name = id+ "-"+ String.format("%03d", index);
                        if (!FileUtil.file(String.format(filUrl,title,name)).exists()) {
                            HttpUtil.downloadFile(imgUrl, FileUtil.file(String.format(filUrl,title,name)));
                            log.info("{} {}", imgUrl,  String.format(filUrl,title,name));
                        } else {
                            log.info("文件已存在 {}", String.format(filUrl,title,name));
                        }
                    } catch (Exception e) {
                        log.info(e.getMessage());
                    }
                }
                index++;
            }
        }
        List<String> nextUrl = Xsoup.select(document,"//*[@class=\"panel-body\"]/div/div/article").list();

        for (String item: nextUrl) {
            if (item.contains("onclick=\"jump('")) {
                String  urlNum = item.substring(item.indexOf("t=") + 2, item.indexOf("')\">"));
                Boolean addBool = set.add(urlNum);
                executorMiddleService.execute(() ->
                        http("http://youke.cn-idvhliy.qiniudns.com.zhaowho.cn/qq.php?t=" + urlNum, urlNum, addBool)
                );
            }
        }
        if (ObjectUtils.isEmpty(nextUrl)) {
            log.error("异常结束，没有检索到指定的nextUrl ,{}", str);
        }
//
//
//        for (String item : arr) {
//            if (item.contains("<title>")) {
//                title = item.substring(item.indexOf("<title>")+7, item.lastIndexOf("</title"));
//                title = title.trim();
//                File titleFile = new File(String.format(filUrl,title,0));
//                if (!titleFile.getParentFile().exists()) {
//                    titleFile.getParentFile().mkdirs();
//                }
//            }
//            if (item.contains("<img src=\"")) {
//                String imgUrl = item.substring(item.indexOf("src=\"") + 5, item.lastIndexOf("\""));
//                if (!imgUrl.contains("\"")){
//                    if (bool) {
//                        try {
//                            String name = id+ "-"+ String.format("%03d", index);
//                            if (!FileUtil.file(String.format(filUrl,title,name)).exists()) {
//                                HttpUtil.downloadFile(imgUrl, FileUtil.file(String.format(filUrl,title,name)));
//                                log.info("{} {}", imgUrl, title);
//                            }
//                        } catch (Exception e) {
//                            log.info(e.getMessage());
//                        }
//                    }
//                    index++;
//                }
//            }
//            if (item.contains("onclick=\"jump('")) {
//                String  urlNum = item.substring(item.indexOf("t=") + 2, item.indexOf("')\">"));
//                if (index == 0) {
//                    log.error(title);
//                    List<String> strings = test(str);
//                    if (!ObjectUtils.isEmpty(strings)){
//                        for (String url : strings){
//                            if (bool) {
//                                try {
//                                    String name = id+ "-"+ String.format("%03d", index);
//                                    if (!FileUtil.file(String.format(filUrl,title,name)).exists()) {
//                                        HttpUtil.downloadFile(url, FileUtil.file(String.format(filUrl,title,name).trim()));
//                                        log.info("{} {}", url, title);
//                                    }else {
//                                        log.info("文件已存在 {}", String.format(filUrl,title,name));
//                                    }
//                                } catch (Exception e) {
//                                    log.info(e.getMessage());
//                                }
//                            }
//                            index++;
//                        }
//                    }
//                }
//                Boolean addBool = set.add(urlNum);
//                executorMiddleService.execute(() ->
//                        http("http://youke.cn-idvhliy.qiniudns.com.zhaowho.cn/qq.php?t=" + urlNum, urlNum, addBool)
//                );
//            }
//
//        }
    }

    public static  List<String>  test(String item){
        Pattern p=Pattern.compile("src=\"(.*?)0\"");
        Matcher m=p.matcher(item);
        List<String> urls = new ArrayList<>();
        while(m.find()){
            urls.add(m.group(1) + "0");
        }
        return urls;
    }
}
